﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;

namespace JSonTest.Controllers
{
		public class HomeController : Controller
		{
			
			public JsonResult GetCustomers()
			{
				dataverseEntities db = new dataverseEntities();
				var result = db.Customers.ToList();
				var test = result.Select(x => new
				{
				  Customer_ID = x.customer_id,
					Firstname = x.Firstname,
					Lastname = x.Lastname,
					Telephone = x.Telephone,
					Birthdate = x.Birthdate.ToLongDateString(),
					City = x.City
				});
				return Json(test, JsonRequestBehavior.AllowGet);
			}


			public JsonResult GetOrders()
			{
					dataverseEntities db = new dataverseEntities();
					var result = db.Orders.ToList();
					var test = result.Select(x => new
					{
							Order_Number = x.order_number,
							Order_Date = x.order_date.ToLongDateString(),
							Customer = x.Customers.Firstname
					});
					return Json(test, JsonRequestBehavior.AllowGet);
			}


			public JsonResult GetItems()
			{
					dataverseEntities db = new dataverseEntities();
					var result = db.Items.ToList();
					var test = result.Select(x => new
					{
						Item_id = x.item_id,
						Name = x.item_name,
						Unit = x.item_unit,
						Quantity = x.item_quantity,
						OrderNo = x.order_number
					});
					return Json(test, JsonRequestBehavior.AllowGet);
			}


			//public JsonResult GetOrderItems(int? id)
			//{
			//		dataverseEntities db = new dataverseEntities();
			//		Orders result = db.Orders.Find(id);
			//		var items = result.Items;

			//		var test = items.Select(x => new
			//		{
			//				Item_Name = x.item_name,
			//				Item_Unit = x.item_unit,
			//				Item_Quantity = x.item_quantity
			//		});
			//		return Json(test, JsonRequestBehavior.AllowGet);
			//}



				public ActionResult Index()
				{
						return View();
				}

				public ActionResult Orders()
				{
						return View();
				}

				public ActionResult Items()
				{
						return View();
				}


			
				public ActionResult About()
				{
						ViewBag.Message = "Your application description page.";

						return View();
				}

				public ActionResult Contact()
				{
						ViewBag.Message = "Your contact page.";

						return View();
				}
		}
}